export function delayFn(time: number = 200) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
}

export function enumnsToOptions(enumns: {string: any}[]) {
  const options = [];
  // eslint-disable-next-line no-restricted-syntax,guard-for-in
  for (const i in enumns) {
    let value = i;
    if (i === 'true' || i === 'false' || i === '0' || i === '1') {
      value = JSON.parse(i);
    }
    options.push({
      label: enumns[i],
      value: String(value),
    });
  }
  return options;
}

export function setModel(model: any, data: any) {
  const result: any = {};
  for (const key in model) {
    if (Object.hasOwnProperty.call(model, key)) {
      const value = data?.[key]
      if (value !== undefined && value !== null && value !== "") {        
        if (typeof value === 'object') {
          result[key] = JSON.stringify(value, null, 4)
        } else {
          result[key] = value
        }
      } else {
        result[key] = ''
      }
    }
  }
  return { ...result }
}

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
export function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
  const result: Partial<T> = {};
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key) && !keys.includes(key as unknown as K)) {
      result[key] = obj[key];
    }
  }
  return result as Omit<T, K>;
}

export function isSvg(str: any) {
  return String(str).toLowerCase().startsWith('<svg');
}

export function isHttps(str: any) {
  return String(str).toUpperCase().indexOf('HTTPS://') !== -1;
}

export function filterEmptyAttrByObj(obj: any) {
  const result: any = {};
  for (const key in obj) {
    const value = obj[key];
    if (Object.prototype.hasOwnProperty.call(obj, key) && value !== undefined && value !== null && value !== "") {
      result[key] = value;
    }
  }
  return result;
}

export function formatJSON(v: any) {
  if (!v) {
    return undefined;
  }
  try {
    const json = JSON.parse(v);
    if (json?.length) {
      return json;
    }
  } catch (error) {
  }
  return undefined;
}

export function getFilename(str: string) {
  if (isHttps(str)) {
    return str?.substring(str?.lastIndexOf('/'))
  }
  return str;
}

export function isMatchStr(str: string, keyword: string) {
  return String(str).toUpperCase().indexOf(String(keyword).toUpperCase()) !== -1;
}

export function listToTree({
  list,
  parentFiled = 'pid',
  labelFiled = 'label',
  valueFiled = 'value',
  childrenFiled = 'children',
}: any) {
  if (!Array.isArray(list) || !list?.length) {
    return []
  }
  let node: any = null;
  let map: any = {};
  let tree: any = [];
  let i: number = -1;
  for (i = 0; i < list.length; i++) {
    map[list[i].id] = i;
    list[i][childrenFiled] = [];
    list[i].label = list[i][labelFiled] || list[i]['name'];
    list[i].value = list[i][valueFiled] || list[i]['id'];
  }
  for (i = 0; i < list.length; i++) {
    node = list[i];
    if (!!node[parentFiled]) {
      // 如果当前项有父ID，则找到父项的位置并将当前项添加到父项的children数组中
      list[map[node[parentFiled]]][childrenFiled].push(node);
    } else {
      // 如果没有父ID，则为树的根节点
      tree.push(node);
    }
  }
  return tree;
}

export function arrayToText(list: any[], filed = 'label') {
  return list?.map((item) => item[filed])?.join('，')
}